<?php

/**
 * @author: chabonxu
 * @createTime: 2022/08/03 11:12
 * @company: https://www.moonton.com
 * @description: excel sheet
 */

require_once 'PHPExcel/Classes/PHPExcel.php';

function IntToBool($v) {
    if ($v == 0 || $v == '0') {
        return false;
    }

    return true;
}

function BoolToInt($v) {
    if ($v == "true" || $v == true) {
        return 1;
    }

    return 0;
}

class Sheet
{
    const TYPE_INT = 'int';
    const TYPE_STRING = 'string';
    const TYPE_FLOAT = 'float';
    const TYPE_BOOL = 'bool';

    private $name;
    private $data;
    private $fields;

    /**
     * @return mixed
     */
    public function __construct($name) {
        $this->name = $name;
        $this->data = array();
        $this->fields = array();
    }

    /**
     * @param mixed $fields
     */
    public function addField($name, $type, $desc) {
        $this->fields[$name] = array(
            'name' => $name,
            'type' => $type,
            'desc' => $desc,
        );
    }

    public function setSheetField(\PHPExcel $objPHPExcel, $index) {
        if ($index != 0) {
            $objPHPExcel->createSheet($index);
        }
        $objSheet = $objPHPExcel->setActiveSheetIndex($index);
        $objSheet->setTitle($this->getName());

        $cnt = 0;
        foreach ($this->fields as $arr) {
            $objSheet->setCellValueByColumnAndRow($cnt, 1, $arr['name']);
            $objSheet->setCellValueByColumnAndRow($cnt, 2, $arr['desc']);
            $objSheet->setCellValueByColumnAndRow($cnt, 3, $arr['type']);
            $cnt++;
        }
    }
    public function setSheetData(\PHPExcel $objPHPExcel, $index, $data) {
        $objSheet = $objPHPExcel->setActiveSheetIndex($index);
        foreach ($data as $row => $list) {
            if (empty($list)) {
                continue;
            }
            $cnt = 0;
            foreach ($list as $name => $value) {
                $type = $this->fields[$name]['type'];
                $real_v = $value;
                switch ($type) {
                    case Sheet::TYPE_BOOL:
                        $real_v = BoolToInt($value);
                        break;
                }

                $objSheet->setCellValueByColumnAndRow($cnt, $row+4, $real_v);
                $cnt++;
            }
        }
    }
    /**
     * @param mixed $name
     */
    public function setName($name)
    {
        $this->name = $name;
    }
    /**
     * @return mixed
     */
    public function getName()
    {
        return $this->name;
    }
    /**
     * @return mixed
     */
    public function getData()
    {
        return $this->data;
    }

    /**
     * @param array $data
     */
    public function setData($data)
    {
        $name_list = array();
        foreach ($data as $key => $value) {
            if ($key == 0) {
                $name_list = $value;
            }
            if ($key <= 2) {
                continue;
            }
            $arr = array();
            $empty = false;

            foreach ($value as $k => $v) {
                $name = $name_list[$k];
                if ($name == '') {
                    continue;
                }
                // 过滤id为空
                if ($k == 0) {
                    if (empty($v) && $name == "id") {
                        $empty = true;
                        break;
                    }
                }

                if (isset($this->fields[$name])) {
                    $type = $this->fields[$name]['type'];
                    $real_v = $v;
                    switch ($type) {
                        case Sheet::TYPE_INT:
                            $real_v = intval($v);
                            break;
                        case Sheet::TYPE_BOOL:
                            $real_v = IntToBool($v);
                            break;
                        case Sheet::TYPE_STRING:
                            $real_v = sprintf("%s", $v);
                            break;
                        case Sheet::TYPE_FLOAT:
                            $real_v = floatval($v);
                            break;
                    }
                    $arr[$name_list[$k]] = $real_v;
                } else {
                    throw new \ElexException(sprintf("字段不存在：%s %s", $name, implode(",", $name_list)));
                }
            }

            if (!$empty) {
                $this->data[] = $arr;
            }
        }
    }
    /**
     * @return array
     */
    public function getFields()
    {
        return $this->fields;
    }

    /**
     * @param array $fields
     */
    public function setFields($fields)
    {
        $this->fields = $fields;
    }
}